package com.sc.nft.controller;

import cn.hutool.json.JSONUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.UpdateMiniUserMaterialRequest;
import com.sc.nft.dao.MallMemberDao;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.dto.IdRequest;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.WxMiniUserInfo;
import com.sc.nft.entity.dto.UserMallMemberDTO;
import com.sc.nft.entity.minimall.MallUserWallet;
import com.sc.nft.feign.WxMiniUserPointsClient;
import com.sc.nft.feign.WxMiniUserRedeemCardClient;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.MallUserWalletService;
import com.sc.nft.sup.Result;
import com.sc.nft.vo.minimalluser.MiniUserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/ipMall/wxMiniMall/user")
@Api(tags = "小程序个人中心")
public class WxMiniUserController {

    private final WxMiniUserPointsClient wxMiniUserPointsClient;
    private final WxMiniUserRedeemCardClient wxMiniUserRedeemCardClient;
    private final UserInfoDao userInfoDao;
    private final MallMemberDao mallMemberDao;
    private final MallUserWalletService mallUserWalletService;


    /**
     * 个人积分详情
     * @return
     */
    @PostMapping(value = "/get", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "个人中心")
    @UserLogin
    public Result<MiniUserInfoVO> get() {
        WxMiniUserInfo wxUser = UserPool.getWxMiniUser();
        IdRequest idRequest = new IdRequest();
        idRequest.setId(wxUser.getId());
        BigDecimal points = wxMiniUserPointsClient.countNum(idRequest);
        Integer cardNum = wxMiniUserRedeemCardClient.countNum(idRequest);
        UserInfo userInfo = userInfoDao.getById(wxUser.getUserId());
        MallUserWallet mallUserWallet = mallUserWalletService.getMallUserWallet(wxUser.getId());
        MiniUserInfoVO miniUserInfoVO = new MiniUserInfoVO();
        //会员等级
        List<UserMallMemberDTO> userMallMemberList = mallMemberDao.getUserMallMember(wxUser.getUserId());
        if (!CollectionUtils.isEmpty(userMallMemberList)) {
            //根据userMallMemberList多个中memberLv最大取出其中一个
            UserMallMemberDTO userMallMemberDTO = userMallMemberList.stream().max(Comparator.comparing(UserMallMemberDTO::getMemberLv)).get();
            miniUserInfoVO.setMemberName(userMallMemberDTO.getMemberName());
        }
        miniUserInfoVO.setPoints(points);
        miniUserInfoVO.setRedeemCardNum(cardNum);
        miniUserInfoVO.setUserTel(userInfo.getUserTel()!= null ? userInfo.getUserTel().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2") : "");
        miniUserInfoVO.setAvatarImg(userInfo.getAvatarImg());
        miniUserInfoVO.setNickName(userInfo.getNickName());
        if (Objects.nonNull(mallUserWallet)) {
            miniUserInfoVO.setCommission(mallUserWallet.getBalance());
        }
        return Result.success(miniUserInfoVO);
    }


    /**
     * 修改个人信息
     * @return
     */
    @PostMapping(value = "/updateUserMaterial", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "修改个人信息")
    @UserLogin
    public Result updateUserMaterial(@RequestBody UpdateMiniUserMaterialRequest request) {
        WxMiniUserInfo wxUser = UserPool.getWxMiniUser();
        UserInfo userInfo = userInfoDao.getById(wxUser.getUserId());
        userInfo.setId(wxUser.getUserId());
        if (StringUtils.isNotBlank(request.getNickName())) {
            userInfo.setNickName(request.getNickName());
        }
        if (StringUtils.isNotBlank(request.getAvatarImg())) {
            userInfo.setAvatarImg(request.getAvatarImg());
        }
        log.info("修改个人信息：{}", JSONUtil.toJsonStr(request));
        userInfoDao.updateNickNameAndAvatarImg(userInfo);
        return Result.success();
    }


    /**
     * 获取用户id
     * @return
     */
    @PostMapping(value = "/getId", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "获取用户id")
    @UserLogin
    public Result getWxUserId() {
        WxMiniUserInfo wxUser = UserPool.getWxMiniUser();
        return Result.success(wxUser.getId());
    }


}